/**********************************************
** @Author: phy
** @Date:   2017-12-12 14:17:37
** @Last Modified by:   phy
***********************************************/
package controllers

import (
	"fmt"
	//"strconv"
	"strings"
	"time"

	"github.com/astaxie/beego"
	//"WebAdminSys/libs"
	"WebAdminSys/models"
)

type UsersRechargelogController struct {
	BaseController
}

func (self *UsersRechargelogController) List() {
	self.Data["pageTitle"] = "用户充值记录"
	//self.Ctx.WriteString("MangeAdminController.List")
	self.display()
}

func (self *UsersRechargelogController) Add() {
	
}

func (self *UsersRechargelogController) Edit() {
	
}

func (self *UsersRechargelogController) AjaxSave() {

}

func (self *UsersRechargelogController) AjaxDel() {

	Admin_id, _ := self.GetInt("id")
	Admin, _ := models.AdminGetById(Admin_id)
	Admin.UpdateTime = time.Now().Unix()
	Admin.Status = 0
	Admin.Id = Admin_id

	if Admin_id == 1 {
		self.ajaxMsg("超级管理员不允许删除", MSG_ERR)
		return
	}

	if err := Admin.Update(); err != nil {
		self.ajaxMsg(err.Error(), MSG_ERR)
	}
	self.ajaxMsg("", MSG_OK)
}

func (self *UsersRechargelogController) Table() {
	fmt.Println("====================ChartRechargecountController Table======================")
	//列表
	page, err := self.GetInt("page")
	if err != nil {
		page = 1
	}
	limit, err := self.GetInt("limit")
	if err != nil {
		limit = 30
	}

	self.pageSize = limit


	date1:= strings.TrimSpace(self.GetString("date1"));
	date2 := strings.TrimSpace(self.GetString("date2"));
	userID,_ := self.GetInt("userID");
	userType,_ :=  self.GetInt("userType");//.value,
	RechargeType,_ := self.GetInt("RechargeType");//.value

/* 	if currDay !="" {
		t:=time.Now().Unix();
		
		t2 := t + 24*3600 *1;//当天
		fmt.Println(t,t2);
		date1 = beego.UnixTimeToString2(t,"");
		date2 = beego.UnixTimeToString2(t2,"");
	 }*/
	if date1 == "" && date2 == "" {
	}else{
		if date1 == "" {
			date1 = "2006-01-02"
		}
	
		if date2 == "" {
			date2 = "2019-01-02"
		}
	}
	
	//列表
	var uT1,uT2 int64 = 0,0;

	if date1 != "" && date2 !="" {
		uT1 = beego.StringTimeToUnix(date1,"2006-01-02");
		uT2 = beego.StringTimeToUnix(date2,"2006-01-02");
	}
	
	fmt.Println("page",page,"limit",limit,"date1:",date1,uT1,"date2:",date2,uT2);
	fmt.Println("=====================================================================")
	//查询条件
	filters := make([]interface{}, 0)
	if userID >0 {
		filters = append(filters, "USERID", userID)
	}
	if RechargeType >0 {
		filters = append(filters, "PLATFORM", RechargeType)
	}
	
/* 
	LV_Signl_None			= 0,		//普通用户 非贵宾
	LV_Sign_User_Normal	  	= 1,		//贵宾普通
	LV_Sign_User_Gold		= 2,		//贵宾黄金
	LV_Sign_User_Boss	    = 3,		//商人boss 用有此等级的商人 其他商人低等级的商人赠送礼物的交易折扣将返还给此用户 目前规定游戏里面只有一个boss
	LV_Sign_Inside			= 4,		//内部账号 */


	if userType == 0 { //普通账号
		filters = append(filters, "LVSIGN__lt", 4)
	}else if userType == 1{	//内部账号
		filters = append(filters, "LVSIGN__gt", 4)
	}


	result, count := models.UserPurchaseInfoGetList(page, self.pageSize,uT1,uT2, filters...)
	list := make([]map[string]interface{}, len(result))
	var totalMoney = 0;
	var totalPeople = make(map[int]int);
	for k, v := range result {
		row := make(map[string]interface{})
		row["userid"] = v.USERID
		row["nickname"] = v.NICKNAME
		row["amount"] = v.RMB
		row["glodamount"] = v.SCORE

		row["time"] = beego.UnixTimeToString(v.PURCHASETIME,"");
		switch(v.PLATFORM){
		case 1:
			row["platform"] = "App Store";
			break;
		case 2:
			row["platform"] = "安卓";
			break;
		case 3:
			row["platform"] = "PC";
			break;
		}
		
		totalMoney = totalMoney + v.RMB;
		totalPeople[v.USERID] = v.USERID;

		list[k] = row
	}

	var otherData = make(map[string]interface{});
	otherData["totalMoney"] = totalMoney;
	otherData["totalPeople"] = len(totalPeople);
	self.ajaxList(otherData, MSG_OK,count , list)
}
